关于\u001b转义字符

您所在的位置:网站首页 u0006是什么 关于\u001b转义字符

关于\u001b转义字符

2024-05-22 22:31| 来源: 网络整理| 查看: 265

关于\u001b转义字符

在nodejs中有这样一段代码可以使得控制台输出显示颜色,process.stderr.write('\u001b[31m error \u001b[0m :)

下面是摘自维基百科的一段内容:

The original specification only had 8 colors, and just gave them names. The SGR parameters 30-37 selected the foreground color, while 40-47 selected the background. Quite a few terminals implemented "bold" (SGR code 1) as a brighter color rather than a different font, thus providing 8 additional foreground colors. Usually you could not get these as background colors, though sometimes inverse video (SGR code 7) would allow that. Examples: to get black letters on white background use ESC[30;47m, to get red use ESC[31m, to get bright red use ESC[31;1m. To reset colors to their defaults, use ESC[39;49m (not supported on some terminals), or reset all attributes with ESC[0m. SGR code

其中有几个关键词:ESC,SGR code, CSI codes

ESC

Unicode Character 'ESCAPE' (U+001B) 原来U+001B在unicode编码中就是被定义为转义字符 请戳这里:ESC 【备注:Unicode code points appear as U+】

CSI code

most of the sequences are more than two characters and start with the characters ESC and [ (left bracket/0x5B). This sequence is called CSI for Control Sequence Introducer (or Control Sequence Initiator)

SGR code

|Code| Name| Effect| |--|--| |CSI n m|Select Graphic Rendition|Sets SGR parameters, including text color. After CSI can be zero or more parameters separated with; . With no parameters, CSI m is treated as CSI 0 m (reset / normal), which is typical of most of the ANSI escape sequences.|

\u001b 在不同字符集下的编码表现: 适用场景(不同字符集) 编码 HTML Entity (decimal) � HTML Entity (hex) � javascript \033(注意有别于8进制的utf-16) C/C++/Java source code \u001B Python source code u"\u001B"(u开头表示这是一个unicode string) UTF-8 (hex) 0x1B (1b) UTF-8 (binary) 00011011 UTF-8 (octal) 033 UTF-16 (hex) 0x001B (001b) UTF-16 (decimal) 27 UTF-32 (hex) 0x0000001B (1b) UTF-32 (decimal) 27

结论: process.stderr.write('\u001b[31m error \u001b[0m) 前面的\u001b[31m用于设定SGR颜色,后面的\u001b[0m相当于一个封闭标签作为前面SGR颜色的作用范围的结束点标记。 这等价于 process.stderr.write('\033[31m error \033[0m) (注意033前面的** \ **是必须加的,这在javascript中才会被识别为八进制的33,即c++中的\u001B)

维基百科 ANSI转义字符之颜色 玩转 命令行之python版本


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3